using UnityEngine;
using System.Collections;

public class picturecontrol : MonoBehaviour
{

	// Use this for initialization
    PictureState state = PictureState.invisiable;
    float alphspeed = 0f;
    float alphtarget = 0.5f;
    float currentalph = 0f;
    public Texture2D colortexture;
    bool iscolortexture = false;
    Color targetcolor = Color.white;
    float colortimetogo;
    void Start()
    {
        if (iscolortexture)
        {
            transform.position = new Vector3(0.5f, 0.5f, gamecontrol.getDepth());
            colortexture = new Texture2D(1, 1);
            colortexture.SetPixel(1, 1, Color.white);
            colortexture.Apply();
            Rect screen = new Rect(-Screen.width / 2, -Screen.height / 2, Screen.width, Screen.height);
            guiTexture.pixelInset = screen;
            guiTexture.texture = colortexture;

            targetcolor.a = 0;
            guiTexture.color = targetcolor;
            guiTexture.enabled = true;
            show(colortimetogo);
        }
	}
	
	// Update is called once per frame
	void Update () {
        if (state== PictureState.showing)
        {
            currentalph = guiTexture.color.a;
            if (guiTexture.color.a < alphtarget)
            {
                Color tmp = guiTexture.color;
                tmp.a += Time.deltaTime * alphspeed;
                guiTexture.color = tmp;
            }
            else
            {
                state = PictureState.visiable;
                gamecontrol.state = GameState.waiting;
            }
        }

        if (state == PictureState.hiding)
        {
            currentalph = guiTexture.color.a;
            if (guiTexture.color.a > 0)
            {
                Color tmp = guiTexture.color;
                tmp.a -= Time.deltaTime * alphspeed;
                guiTexture.color = tmp;
            }
            else
            {
                gamecontrol.state = GameState.waiting;
                Destroy(gameObject);
            }
        }
	}
	
    public void show(float timetogo)
	{
        Color tmp = guiTexture.color;
        tmp.a = 0f;
        guiTexture.color = tmp;

        if (timetogo == 0)
        {
            alphspeed = float.MaxValue;
        }
        else
        {
            alphspeed = alphtarget / timetogo;
        }
        state = PictureState.showing;
	}

    public void draw(float timetogo, Vector2 center,string path)
    {
        if (gamecontrol.pictureperload.ContainsKey(path))
        {
            Vector3 center3 = center;
            center3.z = gamecontrol.getDepth();
            Texture2D t2 = gamecontrol.pictureperload[path];
            guiTexture.pixelInset = new Rect(-t2.width / 2, -t2.height / 2, t2.width, t2.height);
            guiTexture.texture = t2;
            transform.position = center3;
            show(timetogo);
        }
        else
            StartCoroutine(loadTexture(center, timetogo, path));
    }


    public void hide(float timetogo)
    {
        Debug.Log("called!!");
        Color tmp = guiTexture.color;
        guiTexture.color = tmp;

        if (timetogo == 0)
        {
            alphspeed = float.MaxValue;
        }
        else
        {
            alphspeed = guiTexture.color.a / timetogo;
        }
        state = PictureState.hiding;
    }

    private IEnumerator loadTexture(Vector2 center, float timetogo,string path)
    {
        guiTexture.enabled = false;
        path = "file:/" + Application.dataPath + "/../" + path;
        WWW www = new WWW(path);
        yield return www;
        if (www.error == null)
        {
            Vector3 center3 = center;
            center3.z = gamecontrol.getDepth();
            guiTexture.pixelInset = new Rect(-www.texture.width / 2, -www.texture.height / 2, www.texture.width, www.texture.height);
            guiTexture.texture = www.texture;
            transform.position = center3;
        }
        else
        {
            Debug.Log(www.error);
        }
        guiTexture.enabled = true;
        show(timetogo);
    }


    internal void draw(float timetogo, Color colr)
    {
        iscolortexture = true;
        targetcolor = colr;
        colortimetogo = timetogo;
    }
}

public enum PictureState
{
    invisiable,
    showing,
    visiable,
    moving,
    hiding
}